/**
 * Created by dqw on 2015/12/30.
 */

//列表开始
var dtGridColumns = [
    {
        id: 'id',
        title: 'ID',
        type: 'number',
        columnClass: 'text-center width-100',
        fastSort: false
    },
    {
        id: 'userId',
        type: 'number',
        hideQuery: true,
        'export': false,
        hide: true
    },
    {
        id: 'u.userName',
        title: '姓名',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center width-200',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'u.mobilePhone',
        title: '电话',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'feeScale',
        title: '佣金比例',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: true,
        fastQuery: true,
        fastQueryType: 'lk'
    },
    {
        id: 'sellerType',
        title: '销售类型',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: false
    },
    {
        id: 'parentName',
        title: '上级代理商',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        fastQuery: false
    },
    {
        id: 'allCustomer',
        title: '是否可以查看所有用户',
        type: 'string',
        format: 'yyyy-MM-dd',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (record.allCustomer == 1) {
                return '<span style="background:#00a2ca;padding:2px 10px;color:white;">是</span>';
            }
            if (record.allCustomer == 0) {
                return '<span style="background:#ca3f35;padding:2px 10px;color:white;">否</span>';
            }
        }
    },
    {
        id: 'customerPhone',
        title: '是否可以查看客户电话',
        type: 'string',
        headerClass: 'text-center',
        columnClass: 'text-center',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            if (record.customerPhone == 1) {
                return '<span style="background:#00a2ca;padding:2px 10px;color:white;">是</span>';
            }
            if (record.customerPhone == 0) {
                return '<span style="background:#ca3f35;padding:2px 10px;color:white;">否</span>';
            }
        }
    },
    {
        id: 'verify',
        title: '编辑',
        type: 'string',
        columnClass: 'text-center width-200',
        fastSort: false,
        resolution: function (value, record, column, grid, dataNo, columnNo) {
            var verifyHtml = "<a data-target='#editModal' class='btn btn-sm btn-primary m-r-10' data-toggle='modal' data-no='" + dataNo + "' ><i class='fa fa-edit'></i>&nbsp;编辑&nbsp;</a>";
            var html = "";
            // 判断是否有审核权限
            if ($("#editPermi") && $("#editPermi").val() == 1) {
                html += verifyHtml;
            }
            return html;
        }
    }
];

var dtGridOption = {
    lang: 'zh-cn',
    ajaxLoad: true,
    loadURL: ncGlobal.adminRoot + 'seller/list.json',
    exportFileName: '销售列表',
    columns: dtGridColumns,
    gridContainer: 'dtGridContainer',
    toolbarContainer: 'dtGridToolBarContainer',
    pageSize: 10,
    pageSizeLimit: [10, 20, 50],
    ncColumnsType: {int: ["id", "userId"], BigDecimal: ["feeScale"]}
};

var grid = $.fn.DtGrid.init(dtGridOption);
grid.fastQueryParameters = new Object();
//排序
grid.sortParameter.columnId = 'id';
grid.sortParameter.sortType = 1;
//列表结束

//操作处理开始
var OperateHandle = function () {

    function _bindEvent() {

        //新增对话框初始化
        $("#addModal").on("show.bs.modal", function (event) {
        });

        //编辑对话框初始化
        $("#editModal").on("show.bs.modal", function (event) {
            //清除错误信息
            $(".alert-danger").remove();
            $("#editForm").psly().reset();
            //获取接受事件的元素
            var button = $(event.relatedTarget);
            //获取data 参数
            var datano = button.data('no');
            var modal = $(this);
            //获取列表框中的原始数据
            var gridData = grid.sortOriginalDatas[datano];
            $("#id").val(gridData.id);
            if (gridData.allCustomer == 1) {
                $(".allCustomer").bootstrapSwitch('state', true);
            } else {
                $(".allCustomer").bootstrapSwitch('state', false);
            }
            if (gridData.customerPhone == 1) {
                $(".customerPhone").bootstrapSwitch('state', true);
            } else {
                $(".customerPhone").bootstrapSwitch('state', false);
            }
            $("#feeScale").val(gridData.feeScale * 10000);

        });

        //模糊搜索
        $('#customSearch').click(function () {
            grid.fastQueryParameters = new Object();
            grid.fastQueryParameters['lk_u.user_name'] = $('#keyword').val();
            grid.pager.startRecord = 0;
            grid.pager.nowPage = 1;
            grid.pager.recordCount = -1;
            grid.pager.pageCount = -1;
            grid.refresh(true);
        });
    }

    //外部可调用
    return {
        bindEvent: _bindEvent
    }
}();
//操作处理结束

$(function () {
    //加载列表
    grid.load();
    //页面绑定事件
    OperateHandle.bindEvent();
});